<?php 
session_save_path('tmp');
session_start();
include("../includes/functions.php");
$__page = "adverts";
$self = $_SERVER['PHP_SELF'];
// Variables
$mode = "add";
$aid = $aname = $aurl = $adescr = "";
//-----------------------------------------------------------------------

if(isset($_GET['aid']) && isset($_GET['action']))
{
	$action = $_GET['action'];
	$aid = $_GET['aid'];
	if($action == "del")
	{
		$con->delete("adverts","WHERE aid = '$aid' LIMIT 1");
		$con->delete("astats","WHERE aid = '$aid'");
		$mess = getADMSuccess("Advertisment record/stats has been deleted.");
	}
	elseif($action == "update")
	{
		$mode = "update";
		$q  = $con->select_query("adverts","*","where aid = '$aid'");
		if($r = mysql_fetch_object($q))
		{	
			$aname = $r->aname;
			$aurl = $r->aurl;
			$adescr = $r->adescr;
			//$adescr = str_replace(array("<br>","<br />","<br/>","<Br>","<Br />","<Br/>","<BR>","<BR />","<BR/>","<bR>","<bR />","<bR/>"),"",$r->adescr);
		}
		else
		{
			$mode = "add";
			$errors = "Can not find record.";
		}
	}
}


// Handling Form Submit
	if(isset($_POST['Submit']))
	{
		foreach($_POST as $key => $value) {if($key !== "Submit"){$$key = addslashes(trim($value));}}

		// Validation 
		if(empty($aname) || empty($aurl) || empty($adescr)) {$errors .= li("Please enter all the fields.");}
		
		if(empty($errors))
		{
			$fields = array("aname"=>$aname,"aurl"=>$aurl,"adescr"=>nl2br($adescr));
			
			if($mode == "add")
			{
				$con->insert_record("adverts",$fields);
				$mess = getADMSuccess("New advertisment has been stored.");
			}
			else
			{
				$con->update("adverts",$fields,"where aid = '$aid' LIMIT 1");
				$mess = getADMSuccess("Advertisment has been updated.");
			}
			unset($fields);
			$aname = $aurl = $adescr = "";
			$mode = "add";
		}		
	}	
	

//$qa = $my_db->select_assoc('SELECT * FROM ads');
$list = "";
//while($ra = mysql_fetch_object($qa))
//{
//	$dc = jsCnf("Are you sure you want to delete this advertisment?");
//	$id = $ra->aid;
//	$name = $ra->aname;
//	$url = $ra->aurl;
//	$descr = str_replace(array("<br>","<br />","<br/>","<Br>","<Br />","<Br/>","<BR>","<BR />","<BR/>","<bR>","<bR />","<bR/>")," ",$ra->adescr);
//
//	$q1 = $con->select_query("astats","*","where aid = '$id'");
//	$total_clicks = $con->total_records($q1);
//	$q1 = $con->select_query("astats","DISTINCT ip","where aid = '$id'");
//	$unique_clicks = $con->total_records($q1);
//
//	$list .= "<tr>
//			  	<td>$id</td>
//				<td><a href='$url' target='_blank'>$name</a></td>
//				<td>$descr</td>
//				<td>$unique_clicks</td>
//				<td>$total_clicks</td>
//				<td>
//					<div align=\"center\">
//						  <a href=\"$self?aid=$id&action=update\" title=\"Edit\"><img src=\"images/edit_icon.gif\" alt=\"Edit\" /></a>
//						  &nbsp;&nbsp;
//						  <a href=\"$self?aid=$id&action=del\" title=\"Delete\" $dc><img src=\"images/del_icon.gif\" alt=\"Delete\" /></a>
//					</div>
//				</td>
//			  </tr>";
//}


if(!empty($errors)) {$mess = getADMError($errors);}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

	<meta http-equiv="X-UA-Compatible" content="IE=7" />

	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2" />
	
	<title><?php echo $title; ?></title>
	
    <style type="text/css" media="all">
		@import url("css/style.css");
		@import url("css/jquery.wysiwyg.css");
		@import url("css/facebox.css");
		@import url("css/visualize.css");
		@import url("css/date_input.css");
    </style>
	
	<!--[if lt IE 8]><style type="text/css" media="all">@import url("css/ie.css");</style><![endif]-->
<script language="javascript" src="includes/functions.js"></script>

</head>





<body>
	
	<div id="hld">
	
		<div class="wrapper">		<!-- wrapper begins -->
	
		<?php include("__menu.php");?>
		<!-- #header ends -->
		  <!-- .block ends -->
		 
          
<div class="block">
            <div class="block_head">
              <div class="bheadl"></div>
              <div class="bheadr"></div>
              <h2>Advertisment List</h2>
            </div>
		    <!-- .block_head ends -->
            <div class="block_content">
              <form action="<?php echo $self;?>" method="post" name="update_job">
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td width="5%"><strong>No.</strong></td>
                    <td width="11%"><strong>Name</strong></td>
                    <td width="21%" valign="top"><strong>Descr</strong></td>
                    <td width="5%"><strong>Unique Clicks</strong></td>
                    <td width="5%"><strong>Clicks</strong></td>
                    <td width="5%">&nbsp;</td>
                  </tr>
                  <tr>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td valign="top">&nbsp;</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                    <td><img src="images/edit_icon.gif" width="16" height="16" /> <img src="images/del_icon.gif" width="16" height="16" /></td>
                  </tr>
					<?php echo $list; ?>
                </table>
              </form>
            </div>
		    <!-- .block_content ends -->
	      </div>          
		  <!-- .block ends -->
		  <!-- .block ends -->
		  <!-- .block.small.left ends -->
		  <!-- .block.small.right ends -->
		  <!-- .block ends -->
		  <!-- .block ends -->
          <!-- .leftcol ends -->
          <?php include("__footer.php"); ?>
        </div>
		<!-- wrapper ends -->
		
	</div>		<!-- #hld ends -->
	<?php include("__footer_js.php");?>    
</body>
</html>